// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/search/instant_service.h"

#include <vector>

#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/mock_callback.h"
#include "build/build_config.h"
#include "chrome/browser/search/instant_service_observer.h"
#include "chrome/browser/search/instant_unittest_base.h"
#include "chrome/browser/themes/test/theme_service_changed_waiter.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/search/instant_types.h"
#include "chrome/common/url_constants.h"
#include "components/ntp_tiles/features.h"
#include "components/ntp_tiles/ntp_tile.h"
#include "components/ntp_tiles/section_type.h"
#include "components/search/ntp_features.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/native_theme/test_native_theme.h"
#include "url/gurl.h"

namespace {

class MockInstantServiceObserver : public InstantServiceObserver {
 public:
  MOCK_METHOD1(NtpThemeChanged, void(NtpTheme));
  MOCK_METHOD1(MostVisitedInfoChanged, void(const InstantMostVisitedInfo&));
};

class MockInstantService : public InstantService {
 public:
  explicit MockInstantService(Profile* profile) : InstantService(profile) {}
  ~MockInstantService() override = default;

  MOCK_METHOD0(ResetCustomBackgroundNtpTheme, void());
};
}  // namespace

using InstantServiceTest = InstantUnitTestBase;

TEST_F(InstantServiceTest, GetNTPTileSuggestion) {
  ntp_tiles::NTPTile some_tile;
  some_tile.url = GURL("https://foo.com");
  some_tile.title = u"Foo";
  some_tile.favicon_url = GURL("https://foo.com/favicon");
  ntp_tiles::NTPTilesVector suggestions{some_tile};

  std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector> suggestions_map;
  suggestions_map[ntp_tiles::SectionType::PERSONALIZED] = suggestions;

  instant_service_->OnURLsAvailable(suggestions_map);

  auto items = instant_service_->most_visited_info_->items;
  ASSERT_EQ(1, (int)items.size());
  EXPECT_EQ("https://foo.com/", items[0].url);
  EXPECT_EQ(u"Foo", items[0].title);
  EXPECT_EQ("https://foo.com/favicon", items[0].favicon);
}

TEST_F(InstantServiceTest, TestNoNtpTheme) {
  instant_service_->theme_ = nullptr;
  EXPECT_NE(nullptr, instant_service_->GetInitializedNtpTheme());
}

class InstantServiceThemeTest : public InstantServiceTest {
 public:
  InstantServiceThemeTest() {}

  InstantServiceThemeTest(const InstantServiceThemeTest&) = delete;
  InstantServiceThemeTest& operator=(const InstantServiceThemeTest&) = delete;

  ~InstantServiceThemeTest() override {}

  ui::TestNativeTheme* theme() { return &theme_; }

 private:
  ui::TestNativeTheme theme_;
};

TEST_F(InstantServiceTest, SetNTPElementsNtpTheme) {
  // Check defaults when no theme and no custom backgrounds is set.
  NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
  EXPECT_FALSE(theme->logo_alternate);

  // Install colors, theme update should trigger SetNTPElementsNtpTheme() and
  // update NTP themed elements info.
  ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile());
  test::ThemeServiceChangedWaiter waiter(theme_service);
  theme_service->BuildAutogeneratedThemeFromColor(SK_ColorRED);
  waiter.WaitForThemeChanged();

  theme = instant_service_->GetInitializedNtpTheme();
  EXPECT_TRUE(theme->logo_alternate);
}
